﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using Common.InstanceManager;
using LayersDotNet.Entity;

namespace LayersDotNet.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            GridView1.DataSource = WebApp.DataAccess.BankDA.GetAllAsHashSet();
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Bank bank = new Bank() { Name = "Bank " + DateTime.Now.ToUniversalTime() };

            int newId = -1;
            WebApp.DataAccess.BankDA.Insert(bank, out newId);

            Refresh();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Label1.Text = "Bank Count: " + WebApp.BusinessLogic.TransactionBL.GetBankCount().ToString();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = WebApp.DataAccess.BankDA.WhereAsHashSet(b => (b.Id % 5) == 0);
            GridView1.DataBind();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            WebApp.DataAccess.CustomerDA.RemoveBank();
            WebApp.DataAccess.BankDA.DeleteAll();

            GridView1.DataSource = WebApp.DataAccess.BankDA.GetAll();
            GridView1.DataBind();

            Refresh();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Name = "Name1";
            customer.Address = "Address1";
            customer.Bank = WebApp.DataAccess.BankDA.GetAll().First();

            WebApp.DataAccess.CustomerDA.Insert(customer);

            RefreshCustomers();
        }

        private void RefreshCustomers()
        {
            GridView2.DataSource = WebApp.DataAccess.CustomerDA.GetAll();
            GridView2.DataBind();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            RefreshCustomers();
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Name = "Name1";
            customer.Address = "Address1";
            customer.Bank = new Bank() { Name="New Bank" };
            customer.AccountNumber = Guid.NewGuid().ToString();

            WebApp.DataAccess.CustomerDA.Insert(customer);

            RefreshCustomers();
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            WebApp.DataAccess.CustomerDA.DeleteAll();

            RefreshCustomers();
        }
    }
}
